package com.shunhe.zs_pda.util;

import java.util.List;
import java.util.Map;

public class CheckUtil {
    private static final String SEP1 = ",";
    private static final String SEP2 = "|";
    private static final String SEP3 = "=";
    private static final String SEP4 = ">";

    /**
     * <strong>判断一个字符序列是否为空</strong>
     * <ul>
     * <li>如果字符序列对象为空则返回true</li>
     * <li>如果字符序列所有字符均为空格则返回true</li>
     * </ul>
     *
     * @param cs 需要判断的字符序列对象
     * @return 结果为空返回true，否则为false
     */
    public static boolean stringIsBlank(CharSequence cs) {
        if (cs == null) {
            return true;
        }

        int strlen = cs.length();
        if (strlen == 0) {
            return true;
        }

        if ("null".equals(cs)) {
            return true;
        }

        for (int i = 0; i < strlen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }

        return true;
    }

    /**
     * <strong>判断一个字符序列是否为空</strong>
     * <ul>
     * <li>如果字符序列对象为空则返回0</li>
     * <li>如果字符序列所有字符均为空格则返回0</li>
     * </ul>
     *
     * @param cs 需要判断的字符序列对象
     * @return 结果为空返回0，否则为字符串
     */
    public static String stringIsBlankStr(CharSequence cs) {
        if (cs == null) {
            return "0";
        }

        int strlen = cs.length();
        if (strlen == 0) {
            return "0";
        }

        if ("null".equals(cs)) {
            return "0";
        }

        for (int i = 0; i < strlen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return cs.toString();
            }
        }

        return "0";
    }

    /**
     * 判断一个List集合是否为空
     */
    public static <T> boolean isListEmpty(List<T> list) {
        return list == null || list.size() <= 0;
    }

    /**
     * 判断是否存在某个字符串
     */
    public static boolean isContains(String str,String title) {
        if(str.indexOf(title)!=-1){
            return true;
        }else{
            return false;
        }
    }
    /**
     * List转换String
     *
     * @param list
     *            :需要转换的List
     * @return String转换后的字符串
     */
    public static String ListToString(List<?> list) {
        StringBuffer sb = new StringBuffer();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == null || list.get(i) == "") {
                    continue;
                }
                // 如果值是list类型则调用自己
                if (list.get(i) instanceof List) {
                    sb.append(ListToString((List<?>) list.get(i)));
                    sb.append(SEP4);
                } else if (list.get(i) instanceof Map) {
                    sb.append(MapToString((Map<?, ?>) list.get(i)));
                    sb.append(SEP4);
                } else {
                    sb.append(list.get(i));
                    if (i != list.size()-1){
                        sb.append(SEP4);
                    }

                }
            }
        }
        return "分类:" + sb.toString();
    }
    /**
     * List转换String
     *
     * @param list
     *  :需要转换的List
     *  str 分隔符
     * @return String转换后的字符串
     */
    public static String ListToString(List<?> list,String str) {
        StringBuffer sb = new StringBuffer();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == null || list.get(i) == "") {
                    continue;
                }
                // 如果值是list类型则调用自己
                if (list.get(i) instanceof List) {
                    sb.append(ListToString((List<?>) list.get(i)));
                    sb.append(str);
                } else if (list.get(i) instanceof Map) {
                    sb.append(MapToString((Map<?, ?>) list.get(i)));
                    sb.append(str);
                } else {
                    sb.append(list.get(i));
                    if (i != list.size()-1){
                        sb.append(str);
                    }

                }
            }
        }
        return sb.toString();
    }
    /**
     * Map转换String
     *
     * @param map
     *            :需要转换的Map
     * @return String转换后的字符串
     */
    public static String MapToString(Map<?, ?> map) {
        StringBuffer sb = new StringBuffer();
        // 遍历map
        for (Object obj : map.keySet()) {
            if (obj == null) {
                continue;
            }
            Object key = obj;
            Object value = map.get(key);
            if (value instanceof List<?>) {
                sb.append(key.toString() + SEP1 + ListToString((List<?>) value));
                sb.append(SEP2);
            } else if (value instanceof Map<?, ?>) {
                sb.append(key.toString() + SEP1
                        + MapToString((Map<?, ?>) value));
                sb.append(SEP2);
            } else {
                sb.append(key.toString() + SEP3 + value.toString());
                sb.append(SEP2);
            }
        }
        return "M" + sb.toString();
    }
}
